/*
 * $Id: $
 */
package org.rcgwt.client.ui.grid.event;

import com.google.gwt.event.shared.GwtEvent;
import com.google.gwt.event.shared.HasHandlers;

/**
 * 
 * @author Olivier Oeuillot (latest modification by $Author: $)
 * @version $Revision: $ $Date: $
 */
public class PropertyChangedEvent extends GwtEvent<PropertyChangedHandler> {

    private static Type<PropertyChangedHandler> TYPE;

    private final String propertyName;

    private final Object oldValue;

    private final Object newValue;

    private PropertyChangedEvent(String propertyName, Object oldValue,
            Object newValue) {
        this.propertyName = propertyName;
        this.oldValue = oldValue;
        this.newValue = newValue;
    }

    public static Type<PropertyChangedHandler> getType() {
        if (TYPE == null) {
            TYPE = new Type<PropertyChangedHandler>();
        }
        return TYPE;
    }

    @Override
    public final Type<PropertyChangedHandler> getAssociatedType() {
        return TYPE;
    }

    public static <S extends HasPropertyChangedHandlers & HasHandlers> void fire(
            S source, String propertyName, Object oldValue, Object newValue) {
        if (TYPE != null) {
            PropertyChangedEvent event = new PropertyChangedEvent(propertyName,
                    oldValue, newValue);
            source.fireEvent(event);
        }
    }

    @Override
    protected void dispatch(PropertyChangedHandler handler) {
        handler.onPropertyChange(this);
    }

    public String getPropertyName() {
        return propertyName;
    }

    public Object getOldValue() {
        return oldValue;
    }

    public Object getNewValue() {
        return newValue;
    }

}
